package com.sky.service.impl;

import com.sky.config.Calibration;
import com.sky.entity.SetmealDish;
import com.sky.exception.ParityFailureException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.setmeal_dishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class setmeal_dishServiceImpl implements setmeal_dishService {
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public void saveSetmealDish(List<SetmealDish> setmealDish) {
        //1,校验数据
        log.info("保存套餐菜品：{}", setmealDish);
        SetmealDish setmealDish1 = new SetmealDish();
        for (SetmealDish dish : setmealDish) {
            setmealDish1.setSetmealId(dish.getSetmealId());
            setmealDish1.setDishId(dish.getDishId());
            setmealDish1.setName(dish.getName());
            setmealDish1.setPrice(dish.getPrice());
            setmealDish1.setCopies(dish.getCopies());
            setmealDish1.setId(dish.getId());
        }
        //2,保存套餐菜品
        setmealDishMapper.saveSetmealDish(setmealDish1);
    }

    /**
     * 删除套餐菜品
     * @param id
     */
    @Override
    public void deleteByIds(Long id) {
        //校验数据
        if (Objects.isNull(id)) {
            throw new ParityFailureException("id不能为空哦");
        }
        //删除数据
        setmealDishMapper.deleteById(id);


    }

    /**
     * 根据套餐id查询套餐菜品
     * @param setmealId
     * @return
     */
    @Override
    public SetmealDish getSetmealIdsByDishId(Long setmealId) {
        log.info("根据套餐id查询套餐菜品：{}", setmealId);
        //1.校验数据
        if (Objects.isNull(setmealId)) {
            throw new ParityFailureException("套餐id不能为空哦");
        }
        //2.根据套餐id查询套餐菜品
        SetmealDish setmealDish =setmealDishMapper.getSetmealId(setmealId);
        return setmealDish;
    }
}
